package com.cyt.web.controller.phone;

import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.sys.model.po.SysFile;
import com.cyt.sys.model.vo.SysFileVo;
import com.cyt.sys.service.SysFileService;
import com.cyt.sys.service.SysUserService;
import com.cyt.web.controller.base.PhoneController;
import com.cyt.work.repair.model.vo.AssetWarrantyVo;
import com.cyt.work.repair.service.AssetWarrantyService;
import com.cyt.wx.model.po.WxMember;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/7/9.
 */
@RestController
public class PhoneAssetWarrantyController extends PhoneController {


    @Resource
    private AssetWarrantyService assetWarrantyService;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysFileService sysFileService;



    /**
     * 分页查询
     *
     * @param assetWarrantyVo 接受参数对象
     * @param page 分页数
     * @param limit 每页显示条数
     * @return ReturnResult
     * @author han jun
     * @date 2019-7-8 15:03:04
     */
    //注意这个userId 就是token 然后修改
    @BaseLog(module = "资产报修", classz = "资产报修", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/phone/asset/assetWarrantyList", method = RequestMethod.POST)
    public ReturnResult findVoPage(AssetWarrantyVo assetWarrantyVo, int page, int limit,String token) {
        WxMember wxMember = acceptToken(token);
        assetWarrantyVo.setUserId(wxMember.getId());//这里的userId 代表的是会员的id 就是判断每个用户的的标识
        return assetWarrantyService.findVoPage(assetWarrantyVo, page, limit);
    }

    /**
     * 跳转列表界面
     *
     * @param request 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-7-8 15:03:04
     */
    @BaseLog(module = "资产报修", classz = "资产报修", methods = "跳转到列表界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/phone/work/assetList")
    public ModelAndView getNewlist(HttpServletRequest request,String applyId,String token) {

        ModelAndView view = new ModelAndView("/mobile/asset/assetWarrantyList");
        view.addObject("applyId", applyId);
        view.addObject("token",token);
        return view;
    }

    /**
     * 跳转添加或编辑界面
     *
     * @param token 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-7-8 15:03:04
     */
    @BaseLog(module = "资产报修", classz = "资产报修", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/phone/work/assetEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(String token,String applyId) {
        ModelAndView view = new ModelAndView("/mobile/asset/assetWarrantyEdit");
        view.addObject("token",token);
        view.addObject("applyId",applyId);
        return view;
    }


    /**
     * 保存或更新
     *
     * @param request 请求对象
     * @param assetWarrantyVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-7-8 15:03:04
     */
    @BaseLog(module = "资产报修", classz = "资产报修", methods = "保存或更新对象", businessType = BusinessType.INSERT,
            operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/phone/asset/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, AssetWarrantyVo assetWarrantyVo,String token,String applyId) {
        //根据token 获取当前用户
//        SysUser baseUser=sysUserService.selectUserByToken(userId);
        WxMember wxMember = acceptToken(token);
        return assetWarrantyService.addOrUpdate(assetWarrantyVo, wxMember);
    }

    /**
     * 跳转添加或编辑界面
     *
     * @param assetWarrantyVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-7-8 15:03:04
     */
    @BaseLog(module = "资产报修", classz = "资产报修", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/phone/work/assetDetail", method = RequestMethod.GET)
    public ModelAndView assetEdit(AssetWarrantyVo assetWarrantyVo) {
        ModelAndView view = new ModelAndView("/mobile/asset/assetWarrantyDetail");
        if (!StringUtils.isEmpty(assetWarrantyVo.getId())) {
            AssetWarrantyVo assetWarrantyVo1 = assetWarrantyService.findVoOne(assetWarrantyVo);
            view.addObject("assetWarrantyVo",assetWarrantyVo1);
            SysFileVo sysFileVo = new SysFileVo();
            sysFileVo.setObjectId(assetWarrantyVo1.getObjectId());
            List<SysFile> list;
            if (StringUtils.isEmpty(assetWarrantyVo1.getObjectId())){
                list = new ArrayList<>();
            }else {
                list = (List<SysFile>) sysFileService.list(sysFileVo).getData();
                for (SysFile sysFile:list){
                    sysFile.setPath("/sysFile/showFileByPath?path="+sysFile.getPath());
                }
            }
            view.addObject("list",list);

        }
        return view;
    }


}
